// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "DissolveDustanceFade"
{
    Properties
    {
        _MainTex("Main Texture", 2D) = "white" {}
        _Specular("Specular", Color) = (1,1,1,1)
        _Gloss("Gloss", Range(8,256)) = 8

        _DissTexture ("Dissolve Texture", 2D) = "white" {}
		_DissolveColorAmount("Dissolve Color Amount", Range(0, 1)) = 0.1
		_DissolveColor ("Dissolve Color", Color) = (1,1,1,1)
		_DissolveColorStrength("Dissolve Color Strength", Range(0, 5)) = 2
        _Center("Dissolve Center", Vector) = (0,0,0,0)
        _Distance("Dissolve Distance", Float) = 1
        _Interpolation("Dissolve Interpolation", Range(0,5)) = 1

    }

    SubShader
    {
        Pass
        {
            Tags
            {
                "LightMode" = "ForwardBase"
            }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdbase

            #include "UnityCg.cginc"
            #include "Lighting.cginc"
            #include "AutoLight.cginc"

            sampler2D 		_MainTex;
            sampler2D 		_DissTexture;
            fixed4 			_Specular;
            float 			_Gloss;
			float 			_Distance;
			float4 			_Center;
			half 			_Interpolation;
			half 			_DissolveColorAmount;
			half4 			_DissolveColor;
			half 			_DissolveColorStrength;


            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
                float3 worldNormal : TEXCOORD1;
                float4 worldPos: TEXCOORD2;
                SHADOW_COORDS(3)
            };

            v2f vert(appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.worldNormal = UnityObjectToWorldNormal(v.normal);
                o.worldPos = mul(unity_ObjectToWorld, v.vertex);
                TRANSFER_SHADOW(o);
                return o;
            }

            fixed4 frag(v2f i) : SV_TARGET
            {
                fixed4 albedo = tex2D(_MainTex, i.uv);

                float l = length(_Center.xyz - i.worldPos.xyz);
                float4 clipAmount = saturate(_Distance - l + (tex2D(_DissTexture, i.uv) * _Interpolation * saturate(_Distance))) - 0.5;
                clip(clipAmount);



                fixed4 ambient = albedo * UNITY_LIGHTMODEL_AMBIENT;
                float3 worldLight = normalize(UnityWorldSpaceLightDir(i.worldPos.xyz));
                float3 worldView = normalize(UnityWorldSpaceViewDir(i.worldPos.xyz));
                fixed4 diff = albedo * _LightColor0 * max(0, dot(i.worldNormal, worldLight));
                float3 halfDir = normalize(worldView + worldLight);
                fixed4 spec = albedo * _Specular * pow(max(0, dot(halfDir, i.worldNormal)), _Gloss);
                float shadow = SHADOW_ATTENUATION(i);
                fixed4 col = ambient + (diff + spec) * shadow;

				half colorAmount = step(clipAmount, _DissolveColorAmount);

				half4 dc = _DissolveColor * _DissolveColorStrength * tex2D(_DissTexture, i.uv) * _Interpolation;
				return lerp(col, dc, colorAmount);

            }

            ENDCG
        }


    }

    Fallback "VertexLit"
}